<?php


namespace app\index\controller;
use think\App;
use think\console\Table;
use think\Request;
use think\db;
use think\Controller;
use think\Loader;
use think\Log;

class Auth extends Controller
{
    public $uid;
    public function __construct(App $app = null)
    {
        $this->uid = session('uid');

        if(!$this->uid){
            $this->redirect('/index/Index/login');
        }
    }
    // 更新人脸 更新营业执照
    public function saveBaseinfo(){
        $data = input('post.');
        $where['phone'] = input('post.');
        $info = db("Users")->where($where)->find();
        if($data['is_face']){
            db('Users')->where($where)->update(['is_face'=>1]);
        }
        if($data['url']){
            $datas['post_url'] = $data['url'];
            $where2['user_id'] = $info['id'];
            $where2['data_type'] = 3;
            db('user_doc_list')->where($where2)->update($datas);
        }
        $this->success("操作成功");
    }
    // 更新营业执照
    public function updateCommpany(){
         return view("Auth/updateCommpany");
    }
    public function saveZhizhao(){
        $data = input('post.');
        $where['phone'] = $data['phone'];
        
        $datas['company_name'] = $data['company_name'];
        $datas['legal_person'] = $data['legal_person'];
        $datas['unify_social_code'] = $data['unify_social_code'];
        $datas['business_scope'] = $data['business_scope'];
        
        $user = db('users')->where($where)->find();
        if(!$user){
            $this->error("用户不存在");
        }
        $where2['user_id'] = $user['id'];
        $info = db('company_info')->where($where2)->find();
        if($info){
            
            db('company_info')->where($where2)->update($datas);
            //db('user_doc_list')->where('user_id',$user['id'])->update(['auth_status'=> 1]);
        }else{
            $datas['user_id'] = $user['id'];
            db('company_info')->insert($datas);
        }
        $this->success("操作成功");
    }
    // 烟草证认证
    public function yancao(){
        return view("Auth/yancao");
    }
    public function authYancao(){
        $userid = input('post.userid');
        $stringno = input('post.stringno');

        $authres = authYanchao($stringno);
        if($authres){
          //  db('user_doc_list')->where('data_type',4)->where('user_id',$userid)->update(['auth_time'=>date('Y-m-d H:i:s'), 'auth_status'=>1]);
        }else{
          //  db('user_doc_list')->where('data_type',4)->where('user_id',$userid)->update(['auth_time'=>date('Y-m-d H:i:s'), 'auth_status'=>0]);
        }
        echo_info(0,'查询成功',1,$authres);
    }
    // 待认证用户
    public function index(){
        return view("Auth/index");
    }
    // 认证失败的用户
    public function fail(){
        return view("Auth/fail");
    }
    // 已审核通过用户
    public function cess(){
        return view("Auth/cess");
    }
    // 支付记录
    public function payList(){
        return view("Auth/payList");
    }
    // 主动确认用户已支付
    public function querenPay(){
        $where['id'] = input('post.id');
        db('pay_order')->where($where)->update(['trade_status'=>1]);
        $order = db('pay_order')->where($where)->find();
        $where2['id'] = $order['user_id'];
        $res = db('users')->where($where2)->update(['pay_auth_status'=>1]);
        if($res){
            $this->success("审核成功");
        }else{
            $this->error("审核失败");
        }
    }
    // 重新上传身份证
    public function idcardFail(){
        $where['user_id'] = input('post.id');
        $where['data_type'] = 1;
        $res = db('user_doc_list')->where($where)->update(['auth_status'=>0]);
        if($res){
            db('users')->where('id', input('post.id'))->update(['is_real'=>0]);
            $this->success("设置成功");
        }else{
            $this->error("设置失败");
        }
    }
    // 重新上传营业执照
    public function shenheFailYingye(){
        $where['user_id'] = input('post.id');
        $where['data_type'] = 3;
        $res = db('user_doc_list')->where($where)->delete();
        $where['data_type'] = 4;
        $res = db('user_doc_list')->where($where)->delete();
        if($res){
            $this->success("设置成功");
        }else{
            $this->error("设置失败");
        }
    }
    public function getpayList(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            
            $username = input('get.nickName');
            $phone = input('get.phone');
            $trade_status = input('get.trade_status');
            $where = array();
            if($username){
                $like['nickname'] = $username;
                $users = db('users')->where($like)->find();
                $where['user_id'] = $users['id'];
            }
            if($phone){
                $like['phone'] = $phone;
                $users = db('users')->where($like)->find();

                $where['user_id'] = $users['id'];
            }
          
             if($trade_status == '0' || $trade_status == '1'){
                $where['trade_status'] = $trade_status;
            }
  
            
           
            $count = db('pay_order')->where($where)->count();
            $list = db('pay_order')->where($where)->order('id desc')->limit($page,$pageSize)->select();
           
            foreach ($list as $key => $val){

                if($list[$key]['trade_status'] == 1){
                    $list[$key]['trade_status2'] = "<span style='color: red'>已支付</span>";
                }else{
                    $list[$key]['trade_status2'] = "未支付";
                }
                $list[$key]['pay_type2'] = $list[$key]['pay_type']=1?'微信':'支付宝';
                $where2['id'] = $list[$key]['user_id'];
               
                $users = db('users')->where($where2)->find();
                $list[$key]['nickname'] = $users['nickname'];
                $list[$key]['phone'] = $users['phone'];
                $list[$key]['name'] = $users['name'];
                
            }
            echo_info(0,'查询成功',$count,$list);
        }
    }
    public function getAuth3(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            
            $username = input('get.username');
            $where['is_merchant'] = 1;
            $nickName = input('get.nickName');
            if($username){
                $where['phone'] = $username;
            }
            if($nickName){
                $where['nickName'] = $nickName;
            }
            $count =  db('users')->where('name','not null')->where($where)->count();
            $list = db('users')->where('name','not null')->where($where)->order('id desc')->limit($page,$pageSize)->select();
            foreach ($list as $key => $val){
                if($list[$key]['is_real'] == 1){
                    $list[$key]['is_reals'] = "<span style='color: red'>已实名</span>";
                }else{
                    $list[$key]['is_reals'] = "未实名";
                }
                if($list[$key]['pay_auth_status'] == 1){
                    $list[$key]['pay'] = "<span style='color: red'>已支付</span>";
                }else{
                    $list[$key]['pay'] = "未支付";
                }
                if($list[$key]['is_face'] == 1){
                    $list[$key]['face'] = "<span style='color: red'>人脸通过</span>";
                }else{
                    $list[$key]['face'] = "人脸失败";
                }
                $doc_where['user_id'] = $list[$key]['id'];
                $doc_list = db('user_doc_list')->where($doc_where)->find();
                $list[$key]['addtime'] = $doc_list['create_time'];
                $list[$key]['gender'] = conversionSex($list[$key]['gender']);
                $list[$key]['dailiName'] = getDailiName($list[$key]['phone']);
            }
            echo_info(0,'查询成功',$count,$list);
        }
    }
    public function getAuth2(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            $username = input('get.username');
            $nickName = input('get.nickName');
            $where['is_merchant'] = 2;
            if($username){
               
               
                $where['phone'] = $username;
            }
            if($nickName){
                $where['nickName'] = $nickName;
            }
            $count =  db('users')->where('name','not null')->where($where)->count();
            $list = db('users')->where('name','not null')->where($where)->order('id desc')->limit($page,$pageSize)->select();
            foreach ($list as $key => $val){
                if($list[$key]['is_real'] == 1){
                    $list[$key]['is_reals'] = "<span style='color: red'>已实名</span>";
                }else{
                    $list[$key]['is_reals'] = "未实名";
                }
                if($list[$key]['pay_auth_status'] == 1){
                    $list[$key]['pay'] = "<span style='color: red'>已支付</span>";
                }else{
                    $list[$key]['pay'] = "未支付";
                }
                if($list[$key]['is_face'] == 1){
                    $list[$key]['face'] = "<span style='color: red'>人脸通过</span>";
                }else{
                    $list[$key]['face'] = "人脸失败";
                }
                $doc_where['user_id'] = $list[$key]['id'];
                $doc_list = db('user_doc_list')->where($doc_where)->find();
                $list[$key]['addtime'] = $doc_list['create_time'];
            }
            echo_info(0,'查询成功',$count,$list);
        }
    }
    public function shenheOk(){
        $where['id'] = input('post.id');
        $data['is_merchant'] = 1;
        $data['auth_valid_date'] = date('Y-m-d H:i:s',time()+365*24*60*60);
        $res = db('users')->where($where)->update($data);
        if($res){
            $this->success("审核成功");
        }else{
            $this->error("审核失败");
        }
    }
    // 拒绝用户审核
    public function shenheFail(){
        $where['id'] = input('post.id');
        $data['is_merchant'] = 2;
        $res = db('users')->where($where)->update($data);
        if($res){
            $r = db('users')->where($where)->find();
            $map['user_id'] = $r['id'];
            db('user_doc_list')->where($map)->save();
            $this->success("审核成功");
        }else{
            $this->error("审核失败");
        }
    }
    public function getDetail(){
        $qiu_url = "http://img.lingtingkj.com/";

        $zz= array(0=>'未认证',1=>'认证通过',2=>'认证失败','NULL'=>'未知');
        $where['id'] = input('post.id');
        $userinfo = db('users')->where($where)->find();

        $where2['user_id'] = input('post.id');
        $zhengmian = db('user_doc_list')->where($where2)->where('data_type',1)->find(); // 身份证正面
        $fanmian = db('user_doc_list')->where($where2)->where('data_type',2)->find(); // 身份证反面
        $zhizhao = db('user_doc_list')->where($where2)->where('data_type',3)->find(); // 营业执照
        $yaochao = db('user_doc_list')->where($where2)->where('data_type',4)->find(); // 烟草证
        $shiping = db('user_doc_list')->where($where2)->where('data_type',5)->find(); // 食品许可证
        $company = db('company_info')->where($where2)->find();
        $tobacco = db('tobacco_license_info')->where($where2)->find();
        $data = array();
        $data['name'] = $userinfo['name'];
        $data['identitycard'] = $userinfo['identitycard'];
        $data['valid_date'] = $userinfo['valid_date']; // 身份证有效期
        $data['is_reals'] = $userinfo['is_real']==1?'已实名':'未实名';
        $data['pay'] = $userinfo['pay_auth_status']==1?'已支付':'未支付';
        $data['face'] = $userinfo['is_face']==1?'人脸通过':'人脸失败';
        $data['zhengmian_img'] = $qiu_url.$zhengmian['post_url'];
        $data['fanmian_img'] = $qiu_url.$fanmian['post_url'];
        $data['zhizao_img'] = $qiu_url.$zhizhao['post_url'];
        $data['zhizhao_auth'] = $zz[$zhizhao['auth_status']];
        $data['yaochao_img'] = $qiu_url.$yaochao['post_url'];

        $data['shiping_img'] = $qiu_url.$shiping['post_url'];
        $data['face_auth_img'] = $userinfo['face_auth_img'];
        if($company){
            
            $data['unify_social_code'] = $company['unify_social_code']; // 企业代码
            $data['company_name'] = $company['company_name']; // 企业名称
            $data['company_mold'] = $company['company_mold']; // 企业类型
            $data['business_scope'] = $company['business_scope']; // 经营范围
            $data['company_addr'] = $company['company_addr']; // 经营地址
            $data['legal_person'] = $company['legal_person']; // 法人
            $data['registration_date'] = $company['establish_date']; // 注册时间
    
            // $data['license_key'] = $tobacco['license_key'];
            // $data['company_name2'] = $tobacco['company_name'];
            // $data['principal_name'] = $tobacco['principal_name'];
            // $data['operation_site'] = $tobacco['operation_site'];
            // $data['tolerance_range'] = $tobacco['tolerance_range'];
            // $data['supplier'] = $tobacco['supplier'];
            // $data['yaochao_auth'] = $zz[$yaochao['auth_status']];
        }
        
        echo_info(0,'查询成功',count($data),$data);
    }
    public function getAuth(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            $username = input('get.username');
            $nickName = input('get.nickName');
            $where2['is_merchant'] = 0;
            if($username){
                 $where2['phone'] = $username;
            }
            if($nickName){
                $where2['nickName'] = $nickName;
            }
            $count = db('users')->where('name','not null')->where($where2)->order('id')->count();
            $list = db('users')->where('name','not null')->where($where2)->order('id desc')->limit($page,$pageSize)->select();
            foreach ($list as $key => $val){
               
                if($val['is_real'] == 1){
                    $list[$key]['is_reals'] = "<span style='color: red'>已实名</span>";
                }else{
                    $list[$key]['is_reals'] = "未实名";
                }
                if($val['pay_auth_status'] == 1){
                    $list[$key]['pay'] = "<span style='color: red'>已支付</span>";
                }else{
                    $list[$key]['pay'] = "未支付";
                }
                if($val['is_face'] == 1){
                    $list[$key]['face'] = "<span style='color: red'>人脸通过</span>";
                }else{
                    $list[$key]['face'] = "人脸失败";
                }
                $doc_where['user_id'] = $val['id'];
                $doc_list = db('user_doc_list')->where($doc_where)->find();
               // print_r($doc_list);
                $list[$key]['addtime'] = $doc_list['create_time'];
                $list[$key]['gender'] = conversionSex($list[$key]['gender']);
                $list[$key]['dailiName'] = getDailiName($list[$key]['phone']);
            }
            //die;
            echo_info(0,'查询成功',$count,$list);
        }
    }
}